<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="组合编码" prop="combCode">
        <el-input
          v-model="queryParams.combCode"
          placeholder="请输入组合编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组合名称" prop="combName">
        <el-input
          v-model="queryParams.combName"
          placeholder="请输入组合名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组合类型" prop="combType">
        <el-select v-model="queryParams.combType" placeholder="请选择组合类型" clearable>
          <el-option
            v-for="dict in dict.type.dime_comb_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属科室" prop="dimeGroupsid">
        <el-select v-model="queryParams.dimeGroupsid" filterable clearable placeholder="请选择所属科室" >
          <el-option
              v-for="item in basegroupList"
              :key="item.id"
              :label="item.groupName"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="启用" prop="isValid">
<!--        <el-radio-group v-model="queryParams.isValid">-->
<!--          <el-radio :label="1">是</el-radio>-->
<!--          <el-radio :label="0">否</el-radio>-->
<!--        </el-radio-group>-->
        <el-switch v-model="queryParams.isValid"
                   active-color="#13ce66"
                   inactive-color="#ff4949"
                   :active-value="1"
                   :inactive-value="0">
        </el-switch>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:basecomb:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:basecomb:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:basecomb:remove']"
        >禁用</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:basecomb:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="basecombList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="组合编码" width="120" align="center" prop="combCode" />
      <el-table-column label="组合名称" width="300" align="center" prop="combName" />
      <el-table-column label="组合类型" align="center" prop="combType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dime_comb_type" :value="scope.row.combType"/>
        </template>
      </el-table-column>
      <el-table-column label="适用性别" align="center" prop="applySex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.applySex"/>
        </template>
      </el-table-column>
      <el-table-column label="组合价格" align="center" prop="combPrice" />
      <el-table-column label="排序" align="center" prop="sn" />
      <el-table-column label="描述" align="center" prop="describe" />
      <el-table-column label="所属科室" align="center" prop="groupName" />
<!--      <el-table-column label="所属科室" width="200" align="center" prop="dimeGroupsid" >-->
<!--      <template slot-scope="scope">-->
<!--        <span-->
<!--          v-if="scope.row.dimeGroupsid==item.id" v-for="item in basegroupList" :key="item.id">-->
<!--          {{item.groupName}}-->
<!--        </span>-->
<!--      </template>-->
<!--      </el-table-column>-->
      <el-table-column label="启用" width="80" align="center" >
<!--        <template slot-scope="scope">{{-->
<!--            scope.row.isValid ? "是" : "否"-->
<!--          }}</template>        -->
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isValid"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            disabled
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:basecomb:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:basecomb:remove']"
          >禁用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目组合管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="组合编码：" prop="combCode">
              <el-input v-model="form.combCode" placeholder="请输入组合编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组合名称：" prop="combName">
              <el-input v-model="form.combName" placeholder="请输入组合名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="所属科室：" prop="dimeGroupsid">
              <el-select v-model="form.dimeGroupsid" filterable placeholder="请选择所属科室" style="width: 100%">
                <el-option
                  v-for="item in basegroupList"
                  :key="item.id"
                  :label="item.groupName"
                  :value="item.id"
                  v-if="item.isValid === 1"
                  ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="适用性别：" prop="applySex">
              <el-select v-model="form.applySex" clearable placeholder="请选择适用性别" style="width:100%">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="组合类型：" prop="combType">
              <el-select v-model="form.combType" placeholder="请选择组合类型" clearable style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_comb_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组合价格：" prop="combPrice">
              <el-input-number v-model="form.combPrice" :min="0" label="请输入组合价格" style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="排序：" prop="sn">
              <el-input v-model="form.sn" @input="(v)=>(form.sn=v.replace(/[^\d]/g,''))" maxlength="8" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目描述：" prop="describe">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入项目描述"
                v-model="form.describe">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
         <el-col :span="6">
          <el-form-item label="启用：" prop="isValid">
<!--            <el-radio-group v-model="form.isValid">-->
<!--              <el-radio :label="1">是</el-radio>-->
<!--              <el-radio :label="0">否</el-radio>-->
<!--            </el-radio-group>-->
            <el-switch v-model="form.isValid"
                       active-color="#13ce66"
                       inactive-color="#ff4949"
                       :active-value="1"
                       :inactive-value="0">
            </el-switch>
          </el-form-item>
         </el-col>

          <el-col :span="6">
            <el-form-item label="缺省标记：" prop="defaultTag">
              <el-switch v-model="form.defaultTag"
                         active-color="#13ce66"
                         inactive-color="#ff4949"
                         :active-value="1"
                         :inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row v-if="form.combType!==1">
          <el-form-item label="项目明细：">
            <el-transfer
              filterable
              :titles="['待选择', '已选择']"
              filter-placeholder="关键词搜索"
              v-model="form.dimeBasecombitemIdList"
              :data="filteredBaseitemList">
            </el-transfer>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBasecomb, getBasecomb, delBasecomb, addBasecomb, updateBasecomb } from "@/api/basic/basecomb";
import { listBaseitem } from '@/api/basic/baseitem'
import { listBasegroup } from '@/api/basic/basegroup'

export default {
  name: "Basecomb",
  dicts: ['sys_user_sex', 'dime_comb_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDimeBasecombitem: [],
      //基础项目数据
      baseitemList:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目组合管理表格数据
      basecombList: [],
      //科室列表
      basegroupList: [],
      // 项目细项中间表格数据
      dimeBasecombitemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        combCode: null,
        combName: null,
        combType: null,
        dimeGroupsid: null,
        isValid: 1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        combCode: [
          { required: true, message: "项目编码不能为空", trigger: "blur" }
        ],
        combName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        dimeGroupsid: [
          { required: true, message: "所属科室不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    listBaseitem({pageNum: 1,
      pageSize: 3000}).then(response => {
      this.baseitemList = response.rows;
      this.baseitemList.forEach(item => {
        item.key = item.id
        item.label = item.itemName
      })
    });
    listBasegroup({pageNum: 1,
      pageSize: 500}).then(response => {
      this.basegroupList = response.rows;
    });
  },
  computed: {
    filteredBaseitemList() {
      if (this.form.combType === "01") {
        return this.baseitemList.filter(item => item.itemType === '01');
      } else if (this.form.combType === "02") {
        return this.baseitemList.filter(item => item.itemType === '02');
      } else if (this.form.combType === "03") {
        return this.baseitemList.filter(item => item.itemType === '03');
      } else if (this.form.combType === "04") {
        return this.baseitemList.filter(item => item.itemType === '04');
      } else if (this.form.combType === "05") {
        return this.baseitemList.filter(item => item.itemType === '05');
      } else if (this.form.combType === "06") {
        return this.baseitemList.filter(item => item.itemType === '06');
      } else {
        return this.baseitemList;
      }
    }
  },
  methods: {
    /** 查询项目组合管理列表 */
    getList() {
      this.loading = true;
      listBasecomb(this.queryParams).then(response => {
        this.basecombList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        combCode: null,
        combName: null,
        combType: null,
        extendType: null,
        applySex: null,
        combPrice: null,
        sn: null,
        describe: null,
        dimeGroupsid: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        IsValid: null,
        defaultTag: null
      };
      this.dimeBasecombitemList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.isValid = 1;
      this.title = "添加项目组合管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getBasecomb(id).then(response => {
        if (response.data.dimeBasecombitemList) {
          response.data.dimeBasecombitemIdList = response.data.dimeBasecombitemList.map(item => {
            return item.dimeBaseitemid
          })
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改项目组合管理";
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.dimeBasecombitemList = this.dimeBasecombitemList;
          if (this.form.dimeBasecombitemIdList && this.form.dimeBasecombitemIdList.length > 0) {
            this.form.dimeBasecombitemList = this.form.dimeBasecombitemIdList.map(id => {
              return {
                dimeBaseitemid: id
              }
            })
          }
          if (this.form.id != null) {
            updateBasecomb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBasecomb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 禁用按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认禁用项目组合管理编号为"' + ids + '"的数据项？').then(function () {
        return delBasecomb(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("禁用成功");
      }).catch(() => {
      });
    },
    /** 项目细项中间序号 */
    rowDimeBasecombitemIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 项目细项中间添加按钮操作 */
    handleAddDimeBasecombitem() {
      let obj = {};
      obj.dimeBaseitemid = "";
      this.dimeBasecombitemList.push(obj);
    },
    /** 项目细项中间删除按钮操作 */
    handleDeleteDimeBasecombitem() {
      if (this.checkedDimeBasecombitem.length === 0) {
        this.$modal.msgError("请先选择要删除的项目细项中间数据");
      } else {
        const dimeBasecombitemList = this.dimeBasecombitemList;
        const checkedDimeBasecombitem = this.checkedDimeBasecombitem;
        this.dimeBasecombitemList = dimeBasecombitemList.filter(function (item) {
          return checkedDimeBasecombitem.indexOf(item.index) === -1
        });
      }
    },
    /** 复选框选中数据 */
    handleDimeBasecombitemSelectionChange(selection) {
      this.checkedDimeBasecombitem = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/basecomb/export', {
        ...this.queryParams
      }, `basecomb_${new Date().getTime()}.xlsx`)
    }

  }
};
</script>
